package com.wx.service.impl;

import com.wx.dto.BorrowDTO;
import com.wx.dto.BorrowPageQueryDTO;
import com.wx.entity.Borrow;
import com.wx.exception.BaseException;
import com.wx.mapper.BookMapper;
import com.wx.mapper.BorrowMapper;
import com.wx.mapper.UserMapper;
import com.wx.result.PageResult;
import com.wx.service.BorrowService;
import com.wx.vo.BorrowTimeVO;
import com.wx.vo.BorrowVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BorrowServiceImpl implements BorrowService {
    @Autowired
    private BorrowMapper borrowMapper;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void borrowbook(BorrowDTO borrowDTO) {
        // 查询图书数量是否为空
        Integer count = bookMapper.getCountById(borrowDTO.getBookId());
        if(count==null){
            throw new BaseException("图书不存在");
        }
        if(count < 1){
            throw new BaseException("图书数量为空");
        }

        Borrow borrow = new Borrow();
        //复制实体类相同属性
        BeanUtils.copyProperties(borrowDTO, borrow);

        //补全属性
        borrow.setBorrowTime(LocalDateTime.now());
        //七天后返还书籍
        borrow.setReturnTime(LocalDateTime.now().plusDays(7));
        // is_agree字段，借书需要后端管理员同意(0：否/1：是)
        borrow.setIsAgree(0);
        borrow.setIsReturn(0);
        borrow.setIsFinish(0);
        //用户的借书次数加1
        userMapper.increaseBNum(borrowDTO.getBorrowId());
        //图书数量减1
        bookMapper.deleteOneBook(borrow.getBookId());

        borrowMapper.save(borrow);

    }

    /*根据id查询borrow信息*/
    @Override
    public List<BorrowVO> borrowList(Integer id) {
        List<BorrowVO> borrow = borrowMapper.getBorrowById(id);
        return borrow;
    }

    /*根据id还书*/
    @Override
    public void returnBook(Integer id) {
        // is_return
        borrowMapper.updateReturn(id);

        Borrow borrow = new Borrow();
        borrow.setId(id);
        borrow.setCompletionTime(LocalDateTime.now());
        //设置还书时间为现在
        borrowMapper.updateBorrowInfo(borrow);


        // book++
        bookMapper.increaseOneBook(borrowMapper.selectBookId(id));
        // return_num++
        userMapper.increaseRNum(borrowMapper.selectBorrowId(id));
    }

    @Override
    public List<BorrowVO> borrowListR(Integer id) {
        List<BorrowVO> borrow = borrowMapper.getBorrowByIdR(id);
        return borrow;
    }

    @Override
    public BorrowTimeVO borrowinfo(Integer id) {
        return borrowMapper.getBorrowInfo(id);
    }

    @Override
    public PageResult getBorrowList(BorrowPageQueryDTO borrowPageQueryDTO) {
        //起始索引=（页码-1）*每页记录数      limit 起始索引，页面记录数
        PageResult pageResult = new PageResult();

        borrowPageQueryDTO.setPage((borrowPageQueryDTO.getPage() - 1)*borrowPageQueryDTO.getPageSize());

        pageResult.setTotal(borrowMapper.queryBorrowkCount(borrowPageQueryDTO));
        pageResult.setRecords(borrowMapper.pageQuery(borrowPageQueryDTO));

        return pageResult;
    }

}
